package com.party.controller.user.news;

import com.party.common.Page;
import com.party.common.Tools;
import com.party.model.LearningContent;
import com.party.model.News;
import com.party.service.news.NewsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-10-8
 * Time: 下午10:27
 * To change this template use File | Settings | File Templates.
 */

@Controller
@RequestMapping(value = "/news")
public class NewsController {

    @Resource
    NewsService newsService;
    private int pageSize = 18;
    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user/news/index");
        return modelAndView;
    }

    @RequestMapping(value = "/query")
    @ResponseBody
    public Map<String,Object> query(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String, Object>();
        String pageIndexStr = req.getParameter("pageIndex");
        int pageIndex = 1;
        if (pageIndexStr != null && !"".equals(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }
        News news = new News();
        news.setType("1");//内容类型；1新闻,2公告,3支部风采,4党员事迹,5党建导读
        Page p = this.newsService.getNewsList(news, pageSize, pageIndex);
        map.put("pageCount",p.getTotalPageCount() );
        map.put("tableData",p.getResult());
        return map;
    }

    @RequestMapping(value = "content")
    public ModelAndView content(
            HttpSession session,
            HttpServletRequest request
    ) {
        ModelAndView modelAndView = new ModelAndView();
        News news = new News();
        String idStr = request.getParameter("id");
        if(idStr != null && !"".equals(idStr) && Tools.isNumber(idStr))
        {
            Integer id = Integer.parseInt(idStr);
            news = this.newsService.getNewsByPrimaryKey(id);
        }
        modelAndView.addObject("obj",news);
        byte[]binaryContent=news.getContent();
        String content="";
        if(binaryContent!=null){
            content=new String(binaryContent);
        }

        modelAndView.addObject("content",content);
        modelAndView.setViewName("user/news/content");
        return modelAndView;
    }
}
